//
//  SupplyChainAttestViewController.m
//  Globalstone
//
//  Created by 刘建峰 on 16/7/19.
//  Copyright © 2016年 yuf. All rights reserved.
//

#import "SupplyChainAttestViewController.h"
#import "SuppluChainAttestTableViewCell.h"
#import "LoadingAttestViewController.h"
static NSString * cellIndentifier = @"SuppluChainAttestTableViewCell";
@interface SupplyChainAttestViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSInteger _totle;
}
@property (nonatomic,assign)   NSInteger    pageNO;


@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)NSMutableArray * attestArray;

@end

@implementation SupplyChainAttestViewController
- (void)viewWillAppear:(BOOL)animated{
    [self.tableView.header beginRefreshing];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _attestArray = [NSMutableArray array];
    _pageNO = 1;
    [self initViewLayout];
    [self addRefreshing];
    // Do any additional setup after loading the view.
}


- (void)initViewLayout{
    [self adaptiveViewLayout:self.view];
    [self adaptiveViewLayout:self.navView];
    [_tableView registerNib:[UINib nibWithNibName:cellIndentifier bundle:nil] forCellReuseIdentifier:cellIndentifier];
    _tableView.dataSource = self;
    _tableView.delegate = self;

}

- (IBAction)back:(id)sender {
    [self backBtn];
}
#pragma mark -- 添加刷新数据
- (void)addRefreshing{
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        int64_t delayInSeconds = 8.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            [self.tableView.header endRefreshing];
        });
        self.pageNO = 1;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self getCertificationContainerList];
        });
    }];
    [_tableView.header beginRefreshing];
}
//加载更多数据
- (void)loadMoreData{
    if (_attestArray.count == _totle) {
        [self.tableView.footer endRefreshing];
    }else{
        self.pageNO = self.pageNO + 1;
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [self getCertificationContainerList];
        });
    }
    
}

#pragma mark === 请求认证列表
- (void)getCertificationContainerList{
    NSMutableDictionary  *dic=[NSMutableDictionary dictionary];
    [dic setObject:[Common getLoginInfo][Key_User_userId] forKey:@"museId"];
    [dic setObject:[Common getLoginInfo][Key_User_password] forKey:@"musePwd"];
    [dic setObject:[Common getLoginInfo][Key_User_onlineKey] forKey:@"museOnlineTag"];

    NSString * lan = [InternationalControl userLanguage];
    if ([lan isEqualToString:@"en"]) {
        [dic setObject:@"2" forKey:@"language"];
    }else{
        [dic setObject:@"1" forKey:@"language"];
    }
  
    self.shareApi.delegate=self;
    [self.shareApi  requestOperationWithDic:dic withPath:GS_queryToCertificationContainerList withHttpType:@"POST" withTag:GS_GS_queryToCertificationContainerList];
}


#pragma mark  ==== tableViewDelegate

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _attestArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 178*VerticalRatio();
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SuppluChainAttestTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[SuppluChainAttestTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    NSDictionary * dic= _attestArray[indexPath.row];
    cell.attestNum.text = [NSString stringWithFormat:@"%@",dic[@"orderSysId"]];
    cell.attestName.text = [NSString stringWithFormat:@"%@",dic[@"orderTypeName"]];
    cell.attestType.text = [NSString stringWithFormat:@"%@",dic[@"orderType"]];
    NSInteger type = [[NSString stringWithFormat:@"%@",dic[@"orderType"]]integerValue];
    if (type == 2) {
         cell.attestType.text = @"进口";
    }else{
      cell.attestType.text = @"出口";
    }
    cell.attestAddress.text = [NSString stringWithFormat:@"%@",dic[@"orderAddress"]];
//    cell.goodsType.text = [NSString stringWithFormat:@"%@",dic[@"orderAddress"]];
//    cell.goodsPrice.text = [NSString stringWithFormat:@"%@",dic[@"orderAddress"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UIStoryboard * storyBoard = [UIStoryboard storyboardWithName:@"PersonalStoryboard" bundle:nil];
    LoadingAttestViewController * page = [storyBoard instantiateViewControllerWithIdentifier:@"LoadingAttest"];
    page.type = [[NSString stringWithFormat:@"%@",_attestArray[indexPath.row][@"orderType"]]integerValue];
    page.indentId = [NSString stringWithFormat:@"%@",_attestArray[indexPath.row][@"orderId"]];
    page.attestDic = _attestArray[indexPath.row];
    [[Common getNavigation]pushViewController:page];
//
}



#pragma   mark---- GetADBanner Request Delegate

-(void)fetchDatabaseFinished:(NSMutableDictionary *)database withTag:(NSInteger)tag{
    [_tableView.header endRefreshing];
    [_tableView.footer endRefreshing];
    if (tag==GS_GS_queryToCertificationContainerList) {
        
        if (  [[database valueForKey:@"flag"] integerValue]!=1 ) {
            
            [[CWHUDTips sharedInstance]  showTips:[database valueForKey:@"msg"]  withStatus:1 dismissAfter:1.5];
            
        }else  {
            if ([database[@"result"]isEqual:[NSNull null]]) {
                return;
            }
            _attestArray = database[@"result"][@"list"];
            [_tableView reloadData];
        }
    }
}

-(void)fetchDatabaseFailed:(NSError *)error message:(NSString *)message{
    [_tableView.header endRefreshing];
    [_tableView.footer endRefreshing];
    [self dismissWaitDialog];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
